package br.com.cast.servico;

import java.util.Iterator;
import java.util.List;

import javax.transaction.Transactional;

import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import br.com.cast.excecoes.NotaFiscalJaCadastrada;
import br.com.cast.excecoes.ProdutoJaCadastrado;
import br.com.cast.modelo.NotaFiscal;
import br.com.cast.persistencia.NotaFiscalDAO;

@Component
public class NotaFiscalServico implements INotaFiscalServico {

	@Autowired
	private NotaFiscalDAO notaFiscalDAO;

	public Iterator<NotaFiscal> iterator(long inicio, long total) {

		return notaFiscalDAO.consultaPaginada(inicio, total);
	}

	@Override
	public IModel<NotaFiscal> model(NotaFiscal n) {

		return new CompoundPropertyModel<NotaFiscal>(n);
	}

	@Override
	public long size() {
		return notaFiscalDAO.total(NotaFiscal.class);
	}

	@Override
	public void detach() {
		// TODO Auto-generated method stub

	}

	@Transactional
	public void excluir(NotaFiscal n) {
		notaFiscalDAO.excluir(n);

	}

	@Transactional
	public void salvar(NotaFiscal n) {

		if (notaFiscalDAO.notaFiscalJaCadastrado(n)) {
			throw new NotaFiscalJaCadastrada();
		} else {
			if (n.getId() == null) {
				notaFiscalDAO.salvar(n);
			} else {
				notaFiscalDAO.alterar(n);
			}
		}

	}

	public List<NotaFiscal> buscarTodosProdutos() {
		// TODO Auto-generated method stub
		return null;
	}

}
